package StrategyServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.StrategyDao;
import model.Strategy;

/**
 * Servlet implementation class StrategyServlet
 */
@WebServlet("/UeditorUpdate")
public class UeditorUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UeditorUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	//	response.getWriter().append("Served at: ").append(request.getContextPath());
			request.setCharacterEncoding("UTF-8");
	        response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out =  response.getWriter();
	        //拿到编辑器的内容
	        String content = request.getParameter("editorValue");
	        String name = request.getParameter("name");
	        String sid = request.getParameter("sid");
	        Date time= new java.sql.Date(new java.util.Date().getTime());
	 
	        //如果不为空
	        if(content != null && name != null){
	            //将内容设置进属性
	            Strategy strategy = new Strategy();
	            strategy.setId(Integer.parseInt(sid));
	            strategy.setTime(time);
	            strategy.setName(name);
	            strategy.setInfo(content);
	            try {
	    			StrategyDao.updateStrategy(strategy);
	    		out.print("<script>alert('修改成功');window.location.href='admin/AdminStrategyList.jsp'</script>");
	    		} catch (Exception e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    			out.print("<script>alert('修改失败');window.location.href='admin/AdminStrategyList.jsp'</script>");
	    		}
	        }else {
	        	out.print("<script>alert('请填写内容和标题');window.location.href='admin/AdminStrategyList.jsp'</script>");
	        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
